#pragma once

#include "index/attribute_index/attribute_indexer.h"
#include "file_system/file_reader/mmap_file_reader.h"
#include "file_system/file_reader/block_file_reader.h"
#include "util/memory/array/simple_recycle_array.h"

namespace hawking {
namespace indexlib {

class AttributeDiskIndexer : public AttributeIndexer {
public:
    AttributeDiskIndexer(
        int32_t field_id,
        util::ColumnType field_type,
        std::shared_ptr<AttributeConvertor> attribute_convertor) :
            AttributeIndexer(field_id, field_type, attribute_convertor) {}
    virtual ~AttributeDiskIndexer() = default;

    Status Load(
        const std::string& attr_dir,
        FSFileType data_filetype,
        bool need_increase,
        size_t slot_count_for_update,
        size_t slot_bytes_for_update);

    bool IsVarlen() const = 0;

    const MmapFileReader* OffsetReader() const {
        return (const MmapFileReader*)(offset_reader_.get());
    }

    const MmapFileReader* VarlenMetaReader() const {
        return (const MmapFileReader*)(varlen_metafile_reader_.get());
    }

    std::vector<std::unique_ptr<SimpleRecycleArray>>* DocsUpdator() {
        return &docs_updator_;
    }

protected:
    bool IsUpdated_(size_t offset_in_offsetfile) const {
        return offset_in_offsetfile >= offset_reader_->Length();
    }

    std::unique_ptr<MmapFileReader> offset_reader_;
    std::unique_ptr<MmapFileReader> varlen_metafile_reader_;
    std::unique_ptr<FileReader> data_reader_;
    std::vector<std::unique_ptr<SimpleRecycleArray>> docs_updator_;
    size_t slot_count_for_update_ = 0;
    size_t slot_bytes_for_update_ = 0;

private:
    void InitDocUpdator_(size_t slot_count_for_update, size_t slot_bytes_for_update) {
        slot_count_for_update_ = slot_count_for_update;
        slot_bytes_for_update_ = slot_bytes_for_update;
        docs_updator_.resize(DocCount());
    }
};

}
}